// pages/user/login/login.js
const md5 = require('../../../utils/md5.js')
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        unionID: 1,
        shopName: '白领VIP'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            nowDate: new Date().getTime()
        })
        wx.login({
            success(loginRes) {
                wx.setStorageSync('code', loginRes.code)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    formGo: function(e) {
        wx.setStorageSync('loginFormid', e.detail.formId)
    },

    getPhoneNumber: function(e) {
        const that = this
        if (e.detail.errMsg == 'getPhoneNumber:ok') {
            wx.showLoading({
                title: '登录中...',
            })
            let loginData
            if (wx.getStorageSync('userShare')) {
                loginData = {
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    code: wx.getStorageSync('code'),
                    belong_refer: wx.getStorageSync('userShare'),
                    activity: 785
                }

            } else {
                loginData = {
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    code: wx.getStorageSync('code'),
                    activity: 785
                }
            }
            common.post('Login/phone_bing', loginData).then(function(res) {
                wx.login({
                    success(loginRes) {
                        wx.setStorageSync('code', loginRes.code)
                    }
                })
                if (res.code == 204) {
                    wx.hideLoading()
                    wx.showModal({
                        title: '提示',
                        content: '网络繁忙，请重新登录',
                        showCancel: false,
                        success(res) {}
                    })
                } else {
                    wx.hideLoading()
                    wx.setStorageSync('loginStatus', false)
                    wx.setStorageSync('token', res.token)
                    wx.setStorageSync("roleType", res.role_type)
                    wx.setStorageSync("storeId", res.store_id)
                    wx.setStorageSync("realStoreId", res.real_store_id)
                    wx.setStorageSync("userId", res.user_id)
                    wx.setStorageSync("userPhone", res.phone)
                    //注册formId
                    common.post('User/up_form_id', {
                        form_id: wx.getStorageSync('loginFormid')
                    }).then(function(res) {}).catch(function(e) {})
                    //浏览记录
                    common.post('Userbrowse/add_user_browse', {
                        type: 1,
                        client: 2
                    }).then(function(res) {}).catch(function(e) {})
                    if (res.is_unoid == 0) {
                        that.setData({
                            unionID: 0
                        })
                        wx.setStorageSync('unionID', 0)
                    } else {
                        wx.setStorageSync('unionID', 1)
                        if (wx.getStorageSync('prePageUrl').split('?')[0] == '/pages/index/index' || wx.getStorageSync('prePageUrl') == '/pages/cart/cart' || wx.getStorageSync('prePageUrl') == '/pages/user/userCenter/userCenter') {
                            wx.switchTab({
                                url: wx.getStorageSync('prePageUrl')
                            })
                        } else {
                            wx.redirectTo({
                                url: wx.getStorageSync('prePageUrl')
                            })
                        }
                    }
                }
            }).catch(function(e) {})
        }
    },

    getUserInfo: function(e) {
        const that = this
        if (e.detail.errMsg == 'getUserInfo:ok') {
            wx.showLoading({
                title: '加载中',
            })
            let data = {
                code: wx.getStorageSync('code'),
                phone: wx.getStorageSync('userPhone'),
                wx_sm: 1,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv
            }
            common.post('Login/phone_unionid', data).then(function(res) {
                wx.login({
                    success(loginRes) {
                        wx.setStorageSync('code', loginRes.code)
                    }
                })
                if (res.code == 204) {
                    wx.hideLoading()
                    wx.setStorageSync('unionID', 0)
                    wx.showModal({
                        title: '提示',
                        content: '网络繁忙，请重新授权',
                        showCancel: false,
                        success(res) {}
                    })
                } else {
                    wx.hideLoading()
                    wx.setStorageSync('unionID', 1)
                    if (wx.getStorageSync('prePageUrl').split('?')[0] == '/pages/index/index' || wx.getStorageSync('prePageUrl') == '/pages/cart/cart' || wx.getStorageSync('prePageUrl') == '/pages/user/userCenter/userCenter') {
                        wx.switchTab({
                            url: wx.getStorageSync('prePageUrl')
                        })
                    } else {
                        wx.redirectTo({
                            url: wx.getStorageSync('prePageUrl')
                        })
                    }
                }
            }).catch(function(e) {})
        } else {
            wx.setStorageSync('unionID', 0)
            if (wx.getStorageSync('prePageUrl').split('?')[0] == '/pages/index/index' || wx.getStorageSync('prePageUrl') == '/pages/cart/cart' || wx.getStorageSync('prePageUrl') == '/pages/user/userCenter/userCenter') {
                wx.switchTab({
                    url: wx.getStorageSync('prePageUrl')
                })
            } else {
                wx.redirectTo({
                    url: wx.getStorageSync('prePageUrl')
                })
            }
        }
    },

    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    },

    phoneLogin: function(e) {
        wx.setStorageSync('loginFormid', e.detail.formId)
        wx.navigateTo({
            url: '/pages/user/bindPhone/bindPhone',
        })
    },

    goHome: function() {
        wx.setStorageSync('loginStatus', false)
        wx.switchTab({
            url: '/pages/index/index'
        })
    }
})